package cz.cvut.fit.web.nakupniSeznam.dao;




import javax.persistence.NoResultException;
import javax.persistence.Query;

import cz.cvut.fit.web.nakupniSeznam.data.Category;

public class CategoryDAO extends GenericDAO<Category>{
	
	private static CategoryDAO instance = null;

	public static CategoryDAO getInstance() {
		if (instance == null) {
			instance = new CategoryDAO();
		}
		return instance;
	}

	@Override
	protected String getAllQueryName() {
		return Category.QUERY_CATEGORY_ALL;
	}

	@Override
	protected Class<Category> getEntityClass() {
		return Category.class;
	}
	
	@SuppressWarnings("unchecked")
	public java.util.List<Category> getByOwner(String owner) {
		Query q = this.em.createNamedQuery(Category.QUERY_CATEGORY_BY_OWNER);
		q.setParameter(Category.QUERY_PARAM_OWNER, owner);

		try {
			return (java.util.List<Category>) q.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	public Category getByOwnerAndName(String owner, String name) {
		Query q = this.em.createNamedQuery(Category.QUERY_CATEGORY_BY_OWNER_AND_NAME);
		q.setParameter(Category.QUERY_PARAM_OWNER, owner);
		q.setParameter(Category.QUERY_PARAM_NAME, name);
		try {
			return (Category) q.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		}
	}

}
